package com.iseastar.guojiang.newcabinet;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.AppLogic;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.model.ServiceTaskBean;
import com.shouhuobao.station.R;

/**
 * Created by joker on 2017/12/25.
 * 任务完成
 */
public class TaskFinishActivity extends BaseActivity2 implements View.OnClickListener {

    private String content = "<font color=#4a4a4a>需在</font><font color=#EF4040>16点</font><font color=#4a4a4a>前回仓完成包裹交接</font>";
    private Button mNextTaskBT;
    private ServiceTaskBean serviceTaskBean = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showLoadingDialog(null);
        AppHttp.getInstance().courierQueryNextParcels();
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.activity_task_finish);
        super.findViewById();
        getAppTitle().setCommonTitle("任务完成");
        ((TextView) findViewById(R.id.task_content_tv)).setText(Html.fromHtml(content));
        findViewById(R.id.back_home_bt).setOnClickListener(this);
        mNextTaskBT = (Button) findViewById(R.id.next_task_bt);
        mNextTaskBT.setOnClickListener(this);
        mNextTaskBT.setVisibility(View.GONE);
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            //是否有下一个任务
            case MsgID.courier_query_next_parcel:
                cancelLoadingDialog();
                final ReqResult<ServiceTaskBean> reqResult = JSON.parser(msg.obj, ServiceTaskBean.class);
                if (checkLoginStatus(reqResult)) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            serviceTaskBean = reqResult.getResult();
                            if (serviceTaskBean == null || serviceTaskBean.getTaskId() <= 0) {
                                mNextTaskBT.setVisibility(View.GONE);
                            } else {
                                mNextTaskBT.setVisibility(View.VISIBLE);
                            }
                        }
                    });
                } else {
                    showToast(reqResult.getMessage());
                }
                return true;
            default:
                return super.handleMessage(msg);
        }

    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            //回到首页
            case R.id.back_home_bt:
                AppLogic.courierBackToHomeAndRefresh(getContext());
                finish();
                break;
            //下一个任务
            case R.id.next_task_bt:
                if (serviceTaskBean == null) {
                    return;
                }
                intent = new Intent(getContext(), TaskMainDetailActivity.class);
                intent.putExtra("item", serviceTaskBean);
                intent.putExtra("type", 1);
                startActivity(intent);
                finish();
                break;
        }
    }
}
